/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ins.service;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.WordToPDF;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.modules.ins.dao.InsLogDao;
import com.thinkgem.jeesite.modules.ins.dao.InsTableDao;
import com.thinkgem.jeesite.modules.ins.entity.InsLog;
import com.thinkgem.jeesite.modules.ins.entity.InsTable;
/**
 * 检验报告书-日志Service
 * @author fanqinglong
 * @version 2017-12-25
 */
@Service
@Transactional(readOnly = true)
public class InsLogService extends CrudService<InsLogDao, InsLog> {
	@Autowired
	private InsTableDao insTableDao;
	public InsLog get(String id) {
		return super.get(id);
	}
	
	public List<InsLog> findList(InsLog insLog) {
		return super.findList(insLog);
	}
	
	public Page<InsLog> findPage(Page<InsLog> page, InsLog insLog) {
		insLog.getSqlMap().put("dateSQL", getDateSQL(insLog));
		Page<InsLog> result = super.findPage(page, insLog);
		for(InsLog item : result.getList())
		{
			InsTable table = insTableDao.get(item.getTable().getId());
			item.setTable(table);
		}
		return super.findPage(page, insLog);
	}
	
	@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,readOnly = false, rollbackFor = Exception.class)
	public void save(InsLog insLog) {
		super.save(insLog);
	}
	
	@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,readOnly = false, rollbackFor = Exception.class)
	public void delete(InsLog insLog) {
		super.delete(insLog);
	}
	
	private String getDateSQL(InsLog insLog){
		StringBuffer stringBuffer = new StringBuffer();
		if (!StringUtils.isEmpty(insLog.getDateFrom())) {
			stringBuffer.append(" AND a.create_date >= '"+insLog.getDateFrom()+"' ");
		}
		if (!StringUtils.isEmpty(insLog.getDateTo())) {
			stringBuffer.append(" AND a.create_date <= '"+insLog.getDateTo()+"' ");
		}
		return stringBuffer.toString();
	}
	
	/**
	 * 导出PDF
	 * @param request
	 * @param insLog
	 * @param page
	 * @param isPage
	 * @return
	 * @throws IOException
	 */
	public String exportPDF(HttpServletRequest request,InsLog insLog,Page<InsLog> page,boolean isPage) throws IOException{
		//组装excel数据
		List<List<String>> dataList =  findListForExport(insLog,page,isPage);
		String[] headArray = new String[]{"索引","表名称","模块","操作类型","数据摘要","操作日期","操作人"};
		File savePath = new File(request.getSession().getServletContext().getRealPath("/")+"upload/outFile");
		if (!savePath.exists()) {
			savePath.mkdirs();
		}
		File outFile = new File(savePath, "outFile_"+System.currentTimeMillis()+".xlsx");
		if (outFile.exists()) {
			outFile.delete();
		}
			
		//生成excel
		Map<Integer, Integer> widthMap = new HashMap<>();
		widthMap.put(4, 20);
		widthMap.put(5, 20);
		new ExportExcel("数据操作日志", headArray).setDataListIndiv(dataList).setPrintArea(headArray.length, dataList.size(),widthMap).writeFile(outFile.getAbsolutePath());
		//转换成pdf
		String pdfUrl = "outPDF_"+System.currentTimeMillis()+".pdf";
		//pdf文件的绝对地址
		String pdfAbsUrl = WordToPDF.excelToPdf(request, outFile.getAbsolutePath(), pdfUrl);
		//删除excel
		if (outFile.exists()) {
			outFile.delete();
		}
		return pdfAbsUrl;
	}
	/**
	 * 组装导出数据
	 * @param insLog
	 * @param page
	 * @param isPage
	 * @return
	 */
	private List<List<String>> findListForExport(InsLog insLog,Page<InsLog> page,boolean isPage){
		//需要分页
		if (isPage) {
			insLog.setPage(page);
		}
		List<InsLog> list = dao.findList(insLog);
		List<List<String>> resultList = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			InsLog entity = list.get(i);
			List<String> data = new ArrayList<>();
			//索引
			if ("4".equals(entity.getType()) || "5".equals(entity.getType())) {
				data.add(StringUtils.isEmpty(entity.getData())?"":entity.getData());
			}else {
				data.add(StringUtils.isEmpty(entity.getIndex())?"":entity.getIndex());
			}
			//表名称
			data.add(entity.getTable().getName());
			//模块
			data.add("1".equals(entity.getModule())?"数据表":"数据");
			//操作类型
			switch (entity.getType()) {
			case "1":
				data.add("修改");
				break;
			case "2":
				data.add("删除");
				break;
			case "3":
				data.add("打印");
				break;
			case "4":
				data.add("绑定模板");
				break;
			case "5":
				data.add("解绑模板");
				break;
			case "6":
				data.add("创建");
				break;
			case "7":
				data.add("填报");
				break;
			case "8":
				data.add("增加人员");
				break;
			case "9":
				data.add("移除人员");
				break;
			case "10":
				data.add("分配权限");
				break;

			default:
				break;
			}
			//数据摘要
			if (InsLog.TYPE_ASSIGN_USER.equals(entity.getType()) || InsLog.TYPE_OUT_TABLE.equals(entity.getType())) {
				data.add(entity.getData());
			}else {
				data.add("");
			}
			//操作日期
			data.add(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(entity.getCreateDate()));
			//操作人员
			data.add(entity.getCreateBy().getName());
			resultList.add(data);
		}
		return resultList;
	}
}